<template>
    <div class="app-container">
        <!-- 搜索栏 -->
        <el-form @submit.native.prevent :inline="true" v-show="showSearch">
            <el-form-item label="标签名称">
                <el-input @keyup.enter="handleQuery" v-model="queryParams.keyword" style="width: 200px"
                    placeholder="请输入标签名称" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            </el-form-item>
        </el-form>
        <!-- 操作按钮 -->
        <el-row :gutter="10" class="mb15">
            <el-col :span="1.5">
     <el-button
          type="success"
          plain
          class="el-icon-plus"
          @click="openModel(undefined)"
          >新增</el-button
        >
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain :disabled="tagIdList.length === 0" icon="Delete"
                    @click="handleDelete(undefined)">批量删除</el-button>
            </el-col>
               <RightToolBar @changeShow="changeShowSearch" :showSearch="showSearch" @queryTable="getList"></RightToolBar>
        </el-row>
        <!-- 表格展示 -->
        <el-table border :data="tagList" @selection-change="handleSelectionChange" v-loading="loading">
            <!-- 表格列 -->
            <el-table-column type="selection" width="55" align="center"></el-table-column>
            <!-- 标签名 -->
            <el-table-column prop="tagName" width="300" label="标签名" align="center"></el-table-column>
            <!-- 文章量 -->
            <el-table-column prop="articleCount" label="文章量" align="center"></el-table-column>
            <!-- 创建时间 -->
            <el-table-column prop="createTime" width="300" label="创建时间" align="center">
                <template #default="scope">
                    <div class="create-time">
                        <el-icon>
                            <clock />
                        </el-icon>
                        <span style="margin-left: 10px">{{ formattedDate(scope.row.createTime) }}</span>
                    </div>
                </template>
            </el-table-column>
            <!-- 操作 -->
            <el-table-column width="300" label="操作" align="center">
                <template #default="scope">
       <el-button
            type="warning"
            class="el-icon-edit"
            size="mini"
            plain
            @click="openModel(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            class="el-icon-delete"
            size="mini"
            plain
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
         <Pagination
      :total="count"   
      :page.sync="queryParams.current" 
      :limit.sync="queryParams.size"     
      @pagination="getList" 
    />
        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="addOrUpdate" width="500px" append-to-body>
            <el-form ref="tagFormRef" label-width="100px" :model="tagForm" :rules="rules" @submit.native.prevent>
                <el-form-item label="标签名称" prop="tagName">
                    <el-input placeholder="请输入标签名称" v-model="tagForm.tagName" style="width: 250px;" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="addOrUpdate = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { formatDate } from "@/utils/date";
import {getTagListWithArticle, deleteTag, addTag, updateTag} from '@/api/tag'

export default {
  data() {
    return {
    count: 0,
    showSearch: true,
    loading: false,
    title: "",
    addOrUpdate: false,
    queryParams: {
        current: 1,
        size: 10,
    },
    tagForm: {},
    tagIdList: [],
    tagList: [],
    rules: {
      tagName: [
        { required: true, message: "标签名不能为空", trigger: "blur" },
      ],
    },
    }
  },
  methods: {
    //搜索
    handleQuery() {
        this.getList();
    },
    //获取信息
    getList() {
      this.loading = true;
      getTagListWithArticle(this.queryParams).then((data) => {
        if (data.code == 200) {
          this.tagList = data.data.recordList;
          this.count = data.data.count;
          this.loading = false;
        }
      });
    },
    //多选处理
    handleSelectionChange(selectedItems) {
     this.tagIdList = selectedItems.map((item) => item.id);
    },
    //提交表单
    submitForm() {
      this.$refs.tagFormRef.validate((valid) => {
        if (valid) {
          if (this.tagForm.id != undefined) {
            updateTag(this.tagForm).then((data) => {
              if (data.code == 200) {
                this.$message({
                  type: "success",
                  message: data.msg,
                });
                this.addOrUpdate = false;
                this.getList();
              } else {
                this.$message({
                  type: "danger",
                  message: data.msg,
                });
              }
            });
          } else {
            addTag(this.tagForm).then((data) => {
              if (data.code == 200) {
                this.$message({
                  type: "success",
                  message: data.msg,
                });
                this.addOrUpdate = false;
                this.getList();
              } else {
                this.$message({
                  type: "danger",
                  message: data.msg,
                });
              }
            });
          }
        }
      });
    },
    //打开对话框（新增、修改）
    openModel(data) {
      this.addOrUpdate = true;
      if (data == undefined) {
        this.title = "新增标签";
        this.tagForm = {
          id: undefined,
          tagName: "",
        };
      } else {
        this.title = "修改标签";
        this.tagForm = {
          id: data.id,
          tagName: data.tagName,
        };
      }
    },
    //日期格式化
    formattedDate(date) {
      return formatDate(date);
    },
    handleDelete(id) {
      if (id != undefined) {
        this.tagIdList.push(id);
      }
      this.$confirm("确认删除选中的数据吗？", "提示", {
          type: "warning",
          confirmButtonText: '确定',
          cancelButtonText: '取消',
      })
        .then(() => {
        deleteTag(this.tagIdList).then((data) => {
        if (data.code == 200) {
          this.$message({
            type: "success",
            message: data.msg,
          });
          this.getList();
        }
      });
        })
      .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //改变搜索显示状态
    changeShowSearch(data){
      this.showSearch = data;
    },
  },
  mounted() {
    this.getList();
  },
}
</script>

<style>

</style>